//发货单审核

interface IOrderAuditViewModel extends IBaseViewModel<IOrderAuditQueryTerms> {
    /**
     * ui-grid控制
     */
    gridOptions: uiGrid.IGridOptionsOf<carryOrderListResponse>;
    /**
     * 时间日期格式
     */
    dateFormat: "{format:'YYYY.MM.DD HH:mm'}";
    /**
     * 订单状态下拉
     */
    orderStatusDropDown: ValueListItemResponse[];
    /**
     * 订单编号（单个）
     */
    orderId: string;
    /**
     * 客户单位名称
     */
    clientName: string;
    /**
     * 线路地址
     */
    route: string;
    /**
     * 订单状态
     */
    status: string;
    /**
     * 发货开始时间
     */
    shipStartTime: string;
    /**
     * 发货结束时间
     */
    shipEndTime: string;
    /**
     * 发货地址
     */
    shipAddress: string;
    /**
     * 送货地址
     */
    deliverAddress: string;
    /**
     * 下单起始时间
     */
    placeOrderStartTime: string;
    /**
     * 下单结束时间
     */
    placeOrderEndTime: string;
    /**
     * 编辑整单零单跳转
     */
    editorderAudit: (id: string, len: number, orderStatus: string) => void;
    /**
     *查询
     */
    query: () => void;
    /**
     * 列表高度
     */
    heightA: number;
    /**
     * 调度专员
     */
    dispatcher:string;
    /**
     * 定位状态
     */
    locationStatus:string;
}


/**
 * 查询条件
 */
interface IOrderAuditQueryTerms {
    /**
     * 订单编号（单个）
     */
    orderId: string;
    /**
     * 客户单位名称
     */
    clientName: string;
    /**
     * 线路地址
     */
    route: string;
    /**
     * 订单状态
     */
    status: string;
    /**
     * 发货开始时间
     */
    shipStartTime: string;
    /**
     * 发货结束时间
     */
    shipEndTime: string;
    /**
     * 发货地址
     */
    shipAddress: string;
    /**
     * 送货地址
     */
    deliverAddress: string;
    /**
     * 下单起始时间
     */
    placeOrderStartTime: string;
    /**
     * 下单结束时间
     */
    placeOrderEndTime: string;
}

class OrderAuditController extends ListController<IOrderAuditViewModel, IOrderAuditQueryTerms>{
    gridApi: uiGrid.IGridApiOf<carryOrderListResponse>;
    orderBy: number = 0;
    asc: boolean = true;

    constructor($scope: IOrderAuditViewModel,
        private orderService: IorderService,
        private valueService: IValueService,
        private $ngBootbox: BootboxService,
        private $state: angular.ui.IStateService,
        private $location: ng.ILocationService) {
        super($scope);
        this.init();
    }

    init(): void {
        super.init();
        this.$scope.editorderAudit = this.editorderAudit;
        this.$scope.$on("WildDog:Order13", this.onWilddogOrderEvent);
        this.$scope.queryTerms = {
            orderId: "",
            clientName: "",
            route: "",
            status: "3",
            shipStartTime: "",
            shipEndTime: "",
            shipAddress: "",
            deliverAddress: "",
            placeOrderStartTime: "",
            placeOrderEndTime: ""
        };

        this.$scope.gridOptions = {
            enableSorting: true,
            paginationPageSizes: [10, 20, 30],
            useExternalPagination: true,
            useExternalSorting: true,
            enableColumnResizing: true,

            //单选
            multiSelect: true,
            columnDefs: [
                { displayName: "订单编号", field: 'orderId', cellTemplate: '<div  ng-repeat="item in row.entity.orderList" class="LTL-list">{{item.orderId}}</div>', enableColumnMenu: false, cellTooltip: true, headerTooltip: true, width: '9%' },
                { displayName: "客户单位", field: 'clientName', cellTemplate: '<div  ng-repeat="item in row.entity.orderList" class="LTL-list">{{item.clientName}}</div>', enableColumnMenu: false, cellTooltip: true, headerTooltip: true, width: '12%' },
                { displayName: "发货地址", field: 'startAddress', cellTemplate: '<div  ng-repeat="item in row.entity.orderList" class="LTL-list">{{item.startAddress}}</div>', enableColumnMenu: false, cellTooltip: true, headerTooltip: true, width: '12%' },
                { displayName: '送货地址', field: 'endAddress', cellTemplate: '<div  ng-repeat="item in row.entity.orderList" class="LTL-list">{{item.endAddress}}</div>', enableColumnMenu: false, cellTooltip: true, headerTooltip: true, width: '12%' },
                { displayName: '货物名称', field: 'goodsName', cellTemplate: '<div  ng-repeat="item in row.entity.orderList" class="LTL-list">{{item.goodsName}}</div>', enableColumnMenu: false, cellTooltip: true, headerTooltip: true, enableSorting: false },
                { displayName: '货物数量', field: 'goodsNum', cellTemplate: '<div  ng-repeat="item in row.entity.orderList" class="LTL-list">{{item.goodsNum}}</div>', enableColumnMenu: false, cellTooltip: true, headerTooltip: true, enableSorting: false },
                { displayName: '所需车长', field: 'carLength', cellTemplate: '<div  ng-repeat="item in row.entity.orderList" class="LTL-list">{{item.carLength}}</div>', enableColumnMenu: false, cellTooltip: true, headerTooltip: true, },
                { displayName: '车牌号', field: 'carryCarCode', cellTemplate: '<div style="height:{{39 * row.entity.orderList.length}}px;"class="single-list">{{row.entity.carryCarCode}}</div>', enableColumnMenu: false, cellTooltip: true, headerTooltip: true, },
                { displayName: '调度专员', field: 'dispatcher', cellTemplate: '<div ng-repeat="item in row.entity.orderList" style="height:{{39 * row.entity.orderList.length}}px;" class="LTL-list">{{item.dispatcher}}</div>', enableColumnMenu: false, cellTooltip: true, headerTooltip: true, },
                { displayName: '发货时间', field: 'shipTime', cellTemplate: '<div  ng-repeat="item in row.entity.orderList" class="LTL-list">{{item.shipTime}}</div>', enableColumnMenu: false, cellTooltip: true, headerTooltip: true, width: '7%' },
                { displayName: '下单时间', field: 'placeOrderTime', cellTemplate: '<div  ng-repeat="item in row.entity.orderList" class="LTL-list">{{item.placeOrderTime}}</div>', enableColumnMenu: false, cellTooltip: true, headerTooltip: true, enableSorting: false, width: '7%' },
                { displayName: '订单状态', field: 'orderStatus', cellTemplate: '<div  ng-repeat="item in row.entity.orderList" class="LTL-list">{{item.orderStatus}}</div>', enableColumnMenu: false, cellTooltip: true, headerTooltip: true },
                { displayName: '定位状态', field: 'locationStatus',cellTemplate: '<div ng-if="row.entity.locationStatus == true ">'
                                        + '<div style="height:{{39 * row.entity.orderList.length}}px;"class="single-list">已开通</div>'
                                        + '</div>'
                                        + '<div ng-if="row.entity.locationStatus == false ">'
                                           + '<div style="height:{{39 * row.entity.orderList.length}}px;"class="single-list">未开通</div>'
                                           + '<div>'
                                           + '<div ng-if="row.entity.locationStatus == true " >'
                                           + '<div style="height:{{39 * row.entity.orderList.length}}px;"class="single-list">已开通</div>'
                                           + '<div>', enableColumnMenu: false, cellTooltip: true, headerTooltip: true },
                {
                    displayName: "操作",
                    field: '操作',
                    cellTemplate: '<a class="fa fa-edit primary text-info single-list" title="编辑" ng-click="grid.appScope.editorderAudit(row.entity.id,row.entity.orderList.length,row.entity.orderList[0].orderStatus)"style="height:{{39 * row.entity.orderList.length}}px;"></a>', enableColumnMenu: false, enableSorting: false, width: '6%'
                }
            ],
            onRegisterApi: (gridApi) => {
                this.gridApi = gridApi;
                gridApi.pagination.on.paginationChanged(this.$scope, this.paginationChanged);
                gridApi.core.notifyDataChange("options");
                gridApi.core.on.sortChanged(this.$scope, this.sortChange);
            }
        };

        /*datetimepicker*/
        $('#openStartTime').click(function () {
            $('#shipStartTime').datetimepicker('show');
        });
        $('#openEndTime').click(function () {
            $('#shipEndTime').datetimepicker('show');
        });
        $('#openPOrderStartTime').click(function () {
            $('#placeOrderStartTime').datetimepicker('show');
        });
        $('#openPOrderEndTime').click(function () {
            $('#placeOrderEndTime').datetimepicker('show');
        });

        this.$scope.query = this.query;
        this.query();
        this.loadOrderStatusDropDown();
    }


    /**
     * 加载数据
     */
    loadData(queryTerms: IOrderAuditQueryTerms, skip: number, count: number): void {
        this.orderService.carryOrderList(this.orderBy.toString(), this.asc, queryTerms.orderId,
            queryTerms.clientName, queryTerms.route, queryTerms.shipStartTime, queryTerms.shipEndTime,
            queryTerms.status, queryTerms.placeOrderStartTime, queryTerms.placeOrderEndTime,
            queryTerms.shipAddress, queryTerms.deliverAddress, "", "", "", "", "", "", "", "", "", "", "", "", "","","","", skip, count).then(result => {
                // this.$scope.gridOptions.totalItems = result.data.total;
                this.$scope.gridOptions.data = result.data.data;
                /**
                 * 设置列表高度
                 */
                let gridHArr = 0;
                result.data.data.forEach((item, index) => { gridHArr = gridHArr + item.orderList.length; });
                this.$scope.heightA = (gridHArr * 39) + 96;

                if (skip == 0) {
                    this.$scope.gridOptions.paginationCurrentPage = 1;
                }
                this.$scope.querying = false;
            }, error => {
                this.$scope.querying = false;
            })
    }

    getListCount(): void {
        this.orderService.carrierOrderCount(this.$scope.queryTerms.orderId,
            this.$scope.queryTerms.clientName, this.$scope.queryTerms.route, this.$scope.queryTerms.shipStartTime, this.$scope.queryTerms.shipEndTime,
            this.$scope.queryTerms.status, this.$scope.queryTerms.placeOrderStartTime, this.$scope.queryTerms.placeOrderEndTime,
            this.$scope.queryTerms.shipAddress, this.$scope.queryTerms.deliverAddress, "", "", "", "", "", "", "", "", "", "", "", "", "","","","").then(res => {
                this.$scope.gridOptions.totalItems = res.data.extData;
            })
    }




    localHistory(): void {
        super.localHistory(this.$state);
    }
    getHistory(): void {
        super.getHistory(this.$state, this.$location)
    }
    /**
     * 监听野狗事件
     */
    onWilddogOrderEvent = (event: ng.IAngularEvent, data: any) => {
        this.query();
    }

    /**
     * 订单状态下拉
     */
    loadOrderStatusDropDown(): void {
        this.$scope.orderStatusDropDown = this.valueService.getOrderStatusList().data;
    }

    /**
     * 排序事件触发
     */
    sortChange = (grid: uiGrid.IGridInstanceOf<carryOrderListResponse>, sortColumns: Array<uiGrid.IGridColumnOf<carryOrderListResponse>>) => {
        if (sortColumns.length == 0) {
            this.reload();
        } else {
            let sortItem = sortColumns[0];
            switch (sortItem.name) {
                case "orderId": {
                    this.orderBy = 1;
                } break;
                case "carCode": {
                    this.orderBy = 2;
                } break;
                case "carLength": {
                    this.orderBy = 3;
                } break;
                case "shipTime": {
                    this.orderBy = 4;
                } break;
                case "startAddress": {
                    this.orderBy = 5;
                } break;
                case "endAddress": {
                    this.orderBy = 6;
                } break;
                case "viaAddress": {
                    this.orderBy = 7;
                } break;
                case "contractNum": {
                    this.orderBy = 8;
                } break;
                case "contractStatus": {
                    this.orderBy = 9;
                } break;
                case "oilCardStatus": {
                    this.orderBy = 10;
                } break;
                case "receiptStatus": {
                    this.orderBy = 11;
                } break;
                case "clientName": {
                    this.orderBy = 12;
                } break;
                case "orderStatus": {
                    this.orderBy = 13;
                } break;
            }
            this.asc = sortItem.sort.direction == "asc";
            this.reload();
        }
    }

    editorderAudit(id: string, len: number, orderStatus: string): void {
        if (len == 1) {
            this.$state.go("app.order.orderAuditDetailController", { id: id, name: 'Audit', type: orderStatus })
        } else {
            this.$state.go("app.order.orderAuditBreakBulkDetailController", { id: id, name: 'BreakAudit', type: orderStatus })
        }
    }
}

angular.module("tms").controller("orderAuditController", ["$scope", "orderService", "valueService", "$ngBootbox", "$state", "$location", OrderAuditController]);